package com.ccrfid.rmc6.service.video;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.video.VideoOrgEntity;
import com.ccrfid.rmc6.repository.video.VideoOrgRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class VideoOrgServiceImpl implements VideoOrgService {
    private static final Logger logger = LoggerFactory.getLogger(VideoOrgServiceImpl.class);
    @Autowired
    private VideoOrgRepository videoOrgRepository;
    @Autowired
    private VideoSyncService videoSyncService;

    @Override
    public List<VideoOrgEntity> getVideoOrgList() {
        return videoOrgRepository.getAllBy();
    }

    @Override
    public VideoOrgEntity getVideoOrgBySn(String orgSn) {
        return videoOrgRepository.getByOrgSn(orgSn);
    }

    @Override
    public List<VideoOrgEntity> getSubVideoOrgList(String orgSn) {
        VideoOrgEntity entity = videoOrgRepository.getByOrgSn(orgSn);
        if (entity != null) {
            List<VideoOrgEntity> list = getSubOrgList(entity);
            return list;
        }
        return null;
    }

    @Override
    public List<String> getSubVideoOrgSnList(String orgSn) {
        List<String> orgSnList = new ArrayList<>();
        List<VideoOrgEntity> list = getSubVideoOrgList(orgSn);
        if (list != null && !list.isEmpty()) {
            for (VideoOrgEntity entity : list) {
                orgSnList.add(entity.getOrgSn());
            }
        }
        return orgSnList;
    }

    //递归查找所有子部门
    private List<VideoOrgEntity> getSubOrgList(VideoOrgEntity entity) {
        List<VideoOrgEntity> resultList = new ArrayList<>();
        List<VideoOrgEntity> subOrgs = videoOrgRepository.getAllByParentSn(entity.getOrgSn());
        if (subOrgs != null && !subOrgs.isEmpty()) {
            resultList.addAll(subOrgs);
            for (VideoOrgEntity subEntity : subOrgs) {
                resultList.addAll(getSubOrgList(subEntity));
            }
        }
        return resultList;
    }

    @Override
    public VideoOrgEntity createVideoOrg(VideoOrgEntity entity) throws AppException {
        if (entity.getOrgSn() == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_sn_is_null.getCode(), CustomErrors.org_sn_is_null.getReason(), "");
        }
        if (videoOrgRepository.existsByOrgSn(entity.getOrgSn())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_sn_conflict.getCode(), CustomErrors.org_sn_conflict.getReason(), "");
        }
        if (entity.getParentSn() != null && !videoOrgRepository.existsByOrgSn(entity.getParentSn())) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.pid_not_found.getCode(), CustomErrors.pid_not_found.getReason(), "");
        }
        return videoOrgRepository.save(entity);
    }

    @Override
    public VideoOrgEntity updateVideoOrg(String orgSn, VideoOrgEntity entity) throws AppException {
        if (orgSn == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_sn_is_null.getCode(), CustomErrors.org_sn_is_null.getReason(), "");
        }
        if (!orgSn.equals(entity.getOrgSn())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.different_args.getCode(), CustomErrors.different_args.getReason(), "");
        }
        if (!videoOrgRepository.existsByOrgSn(orgSn)) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_not_found.getCode(), CustomErrors.org_not_found.getReason(), "");
        }
        return videoOrgRepository.save(entity);
    }

    @Override
    public Boolean deleteVideoOrg(String orgSn) throws AppException {
        if (orgSn == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_sn_is_null.getCode(), CustomErrors.org_sn_is_null.getReason(), "");
        }
        if (!videoOrgRepository.existsByOrgSn(orgSn)) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_not_found.getCode(), CustomErrors.org_not_found.getReason(), "");
        } else {
            //删除该部门及其子部门
            deleteOrgAndSubOrgs(videoOrgRepository.getByOrgSn(orgSn));
            return true;
        }
    }

    //递归删除所有子部门，包括自己
    private void deleteOrgAndSubOrgs(VideoOrgEntity entity) {
        if (videoOrgRepository.existsByParentSn(entity.getOrgSn())) {
            //拥有子部门
            List<VideoOrgEntity> subOrgs = videoOrgRepository.getAllByParentSn(entity.getOrgSn());
            for (VideoOrgEntity sub : subOrgs) {
                deleteOrgAndSubOrgs(sub);
            }
        } else {
            //删除部门信息
            videoOrgRepository.deleteByOrgSn(entity.getOrgSn());
        }
    }

    @Override
    public Boolean syncVideoOrgNode(String orgSn) throws AppException {
        VideoOrgEntity orgEntity = videoOrgRepository.getByOrgSn(orgSn);
        if (orgEntity == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_not_found.getCode(), CustomErrors.org_not_found.getReason(), "");
        }

        videoSyncService.syncVideoOrg(orgEntity);
        return true;
    }
}
